import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';

import '../../../../http/bean/http_data.dart';
import '../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../http/model/ajaxsales_model.dart';
import '../../../dialog/dailog.dart';
import '../../works/public/image/ui_image_upload.dart';
import 'public/items/ui_attachment.dart';
import 'public/acom/ui_material_form.dart';
import 'public/items/ui_material.dart';
import 'public/review_submitted_material.dart';

///物料请购详情
class MaterialRequisitionDetailsRoute extends StatefulWidget {
  @override
  _State createState() => _State();

  MaterialInfo? materialInfo;

  MaterialRequisitionDetailsRoute(this.materialInfo);
}

class _State extends State<MaterialRequisitionDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    if (presenter?.mDatas == null) {
      presenter?.mDatas = AppHttpResult<String, String>();
    }

    ///fixme 现在物料请购所有数据都是从上一个页面带过来的。
    presenter?.mDatas?.Material = widget.materialInfo;

    // if (StringUtils.isEmpty(widget.materialInfo?.PurCode)) {
    //   ///请购单号为空,数据从上个页面带过来。
    //   if (presenter?.mDatas == null) {
    //     presenter?.mDatas = AppHttpResult<String, String>();
    //   }
    //   presenter?.mDatas?.Material = widget.materialInfo;
    // } else {
    //   ///请购单号，不为空，数据通过请购单号请求获取
    //   presenter.getContractapplygetinfoWithDelay(context,
    //       callback: () {}); //获取审核详情数据
    // }
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body,
        title: AppStrings.of(context).MaterialRequisition,
        bottoUi: _bottom(context));
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    UiImageUpLoad.clear();
    presenter?.mOldDatas?.Material = null;
    presenter?.mDatas?.Material = null;
    widget?.materialInfo = null;
    AjaxSalesModel.clear(); //清除物料编码数据缓存；
  }

  Widget _body(BuildContext context) {
    var mDatas = widget.materialInfo;
    if (mDatas == null) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    children.add(UiMaterial.build(context, presenter?.mDatas?.Material,
        presenter?.mOldDatas?.Material, setState,
        isPurStartDate: false)); //请购单信息
    // presenter?.mDatas?.Material?.PurDemandList?.forEach((element) {
    //   element?.isToMM = true; //fixme 提交的时候，单位需要转化为毫秒。
    // });
    children.add(UiMaterialForm.build(
        context, presenter?.mDatas?.Material, true)); //物料信息
    var isCanoOeration = presenter?.mDatas?.Material?.isCanoOeration() ?? false;
    if (isCanoOeration) {
      children.add(UiImageUpLoad.build(
          context, presenter?.mDatas?.Material?.getId(),
          title: AppStrings.of(context).UploadEvidence2)); //上传图片
    }
    children.add(UiAttachment.build(
        context, presenter?.mDatas?.Material?.FilePatchOld,
        isDel: true)); //附件
    return UiBase.listView(context, children);
    // return Column(
    //   children: [
    //     Expanded(child: UiBase.listView(context, children)),
    //     Visibility(
    //         child: ReviewSubmittedStateful2(
    //           presenter?.mDatas,
    //           hasSave: presenter?.mDatas?.Material?.isCanoOeration(),
    //           //hasSave 保存
    //           hasSubmit: presenter?.mDatas?.Material?.isCanoOeration(),
    //           //hasSubmit 提交
    //           onlyBack: presenter?.mDatas?.Material?.isOnlyBack(),
    //           //onlyBack 是否只有一个返回键，其他什么操作都没有。
    //           callbackWithT: (subData) {
    //             //fixme 提交参数数据
    //             subData.purDemand = presenter?.mDatas?.Material;
    //           },
    //           callbackWithBoolResult: () {
    //             //fixme 提交数据格式验证
    //             if (StringUtils.isEmptyWithNumber(
    //                 presenter?.mDatas?.Material?.PurEndDate)) {
    //               AppDialog.showDialogWithInfo(context,
    //                   content: AppStrings.of(context)
    //                       .PurchaseDate5); //fixme 要货日期不能为空
    //               return false;
    //             }
    //             var i = 1;
    //             var ii = 1;//fixme 记录第几行
    //             var b = true;
    //             presenter?.mDatas?.Material?.PurDemandList?.forEach((element) {
    //               //fixme 请购申请，有提交和保存的时候，MinNum和DemandNum赋值一样。
    //               //fixme 防止 MinNum与DemandNum不同；物料请购提交服务器使用的申请数量是MinNum这个字段；（请购申请，物料请购时，注意。）
    //               element.MinNum = element.DemandNum;
    //               if (b && StringUtils.isEmptyWithNumber(element?.DemandNum)) {
    //                 ii = i;
    //                 b = false;
    //               }
    //               i++;
    //             });
    //             if (!b) {
    //               AppDialog.showDialogWithInfo(context,
    //                   content: AppStrings.of(context)
    //                       .NumberOfApplications2(ii)); //fixme 申请数量不能为空
    //               return false;
    //             }
    //             return true;
    //           },
    //         ),
    //         visible: presenter?.mDatas != null)
    //   ],
    // );
  }

  ///底部布局；（防止被软键盘弹起）
  Widget? _bottom(BuildContext context) {
    var visible = presenter?.mDatas != null;
    var onlyBack = presenter?.mDatas?.Material?.isOnlyBack();
    var visibility = Visibility(
        child: ReviewSubmittedStateful2(
          presenter?.mDatas,
          hasSave: presenter?.mDatas?.Material?.isCanoOeration(),
          //hasSave 保存
          hasSubmit: presenter?.mDatas?.Material?.isCanoOeration(),
          //hasSubmit 提交
          onlyBack: onlyBack,
          //onlyBack 是否只有一个返回键，其他什么操作都没有。
          callbackWithT: (subData) {
            //fixme 提交参数数据
            subData.purDemand = presenter?.mDatas?.Material;
          },
          callbackWithBoolResult: () {
            //fixme 提交数据格式验证
            if (StringUtils.isEmptyWithNumber(
                presenter?.mDatas?.Material?.PurEndDate)) {
              AppDialog.showDialogWithInfo(context,
                  content:
                      AppStrings.of(context).PurchaseDate5); //fixme 要货日期不能为空
              return false;
            }
            var i = 1;
            var ii = 1; //fixme 记录第几行
            var b = true;
            presenter?.mDatas?.Material?.PurDemandList?.forEach((element) {
              //fixme 请购申请，有提交和保存的时候，MinNum和DemandNum赋值一样。
              //fixme 防止 MinNum与DemandNum不同；物料请购提交服务器使用的申请数量是MinNum这个字段；（请购申请，物料请购时，注意。）
              element.MinNum = element.DemandNum;
              if (b && StringUtils.isEmptyWithNumber(element?.DemandNum)) {
                ii = i;
                b = false;
              }
              i++;
            });
            if (!b) {
              AppDialog.showDialogWithInfo(context,
                  content: AppStrings.of(context)
                      .NumberOfApplications2(ii)); //fixme 申请数量不能为空
              return false;
            }
            return true;
          },
        ),
        visible: visible);
    if (visible) {
      var _height = 150.0;
      if (onlyBack ?? false) {
        _height = 88;
      }
      return Container(
        child: visibility,
        height: _height,
      );
    }
    return null;
  }
}

class _Presenter extends ReviewPresenter {}
